﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace yeqw.FrameWork
{
    public class SerializeHelper
    {
        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static MemoryStream Serialize(object obj)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(ms, obj);
            return ms;
        }
        /// <summary>
        /// Base64反序列化
        /// </summary>
        /// <param name="base64Str"></param>
        /// <returns></returns>
        public static object Deserialize(string base64Str)
        {
            byte[] array = Convert.FromBase64String(base64Str);
            BinaryFormatter bf = new BinaryFormatter();
            using (MemoryStream ms = new MemoryStream(array, 0, array.Length))
            {
                return bf.Deserialize(ms);
            }
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static object Deserialize(byte[] array)
        {
            BinaryFormatter bf = new BinaryFormatter();
            using (MemoryStream ms = new MemoryStream(array, 0, array.Length))
            {
                return bf.Deserialize(ms);
            }
        }
    }
}
